import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../../../../common/models/process/office_center_filter.dart';
import '../../../../../../common/routers/routes.dart';
import '../../../../../../common/style/color.dart';
import '../../../../../../common/utils/log_util.dart';
import '../../../../../../common/widgets/o2_elevated_button.dart';
import '../../../../../../common/widgets/o2_stateless_widget.dart';
import 'index.dart';

class OfficeCenterFilterAttributePage
    extends GetView<OfficeCenterFilterAttributeController> {
  const OfficeCenterFilterAttributePage({Key? key}) : super(key: key);

  static Future<dynamic> openFilter(String? type, OfficeCenterFilterBody? body) async {
    OLogger.d('打开筛选 ${body?.toJson()}');
    return await Get.toNamed(O2OARoutes.appProcessOfficeCenterFilterAttribute,
        arguments: {'type': type, 'result': body?.toJson()});
  }

  // 主视图
  Widget _buildView(BuildContext context) {
    return Obx(() => ListView(
          children: [
            if (controller.state.applicationList.length > 0)
              _filterTypeItem(
                  context,
                  OfficeCenterFilterAttributeType.applicationList,
                  controller.state.applicationList),
            if (controller.state.processList.length > 0)
              _filterTypeItem(
                  context,
                  OfficeCenterFilterAttributeType.processList,
                  controller.state.processList),
            if (controller.state.activityNameList.length > 0)
              _filterTypeItem(
                  context,
                  OfficeCenterFilterAttributeType.activityNameList,
                  controller.state.activityNameList),
            if (controller.state.creatorUnitList.length > 0)
              _filterTypeItem(
                  context,
                  OfficeCenterFilterAttributeType.creatorUnitList,
                  controller.state.creatorUnitList),
            if (controller.state.startTimeMonthList.length > 0)
              _filterTypeItem(
                  context,
                  OfficeCenterFilterAttributeType.startTimeMonthList,
                  controller.state.startTimeMonthList),
            if (controller.state.completedTimeMonthList.length > 0)
              _filterTypeItem(
                  context,
                  OfficeCenterFilterAttributeType.completedTimeMonthList,
                  controller.state.completedTimeMonthList),
            if (controller.state.completedList.length > 0)
              _filterTypeItem(
                  context,
                  OfficeCenterFilterAttributeType.completedList,
                  controller.state.completedList),
          ],
        ));
  }

  Widget _filterTypeItem(
      BuildContext context,
      OfficeCenterFilterAttributeType type,
      List<OfficeCenterFilterAttributeItem> itemValues) {
    String typeName = '';
    switch (type) {
      case OfficeCenterFilterAttributeType.applicationList:
        typeName = 'process_office_center_search_filter_appname'.tr;
        break;
      case OfficeCenterFilterAttributeType.processList:
        typeName = 'process_office_center_search_filter_processname'.tr;
        break;
      case OfficeCenterFilterAttributeType.activityNameList:
        typeName = 'process_office_center_search_filter_activityname'.tr;
        break;
      case OfficeCenterFilterAttributeType.creatorUnitList:
        typeName = 'process_office_center_search_filter_unitname'.tr;
        break;
      case OfficeCenterFilterAttributeType.startTimeMonthList:
        typeName = 'process_office_center_search_filter_recive_month'.tr;
        break;
      case OfficeCenterFilterAttributeType.completedTimeMonthList:
        typeName = 'process_office_center_search_filter_done_month'.tr;
        break;
      case OfficeCenterFilterAttributeType.completedList:
        typeName = 'process_office_center_search_filter_completed_status'.tr;
        break;
    }

    return Obx(() {
      final item = controller.state.filterPicked.value;
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
              padding: const EdgeInsets.only(left: 10, top: 5, bottom: 5),
              child: Text(typeName,
                  style: Theme.of(context).textTheme.titleMedium)),
          Padding(
              padding: const EdgeInsets.only(
                  left: 12, top: 10, bottom: 10, right: 5),
              child: Wrap(
                spacing: 8.0, // 项目之间的水平间距
                runSpacing: 8.0, // 项目之间的垂直间距
                children: itemValues.map((e) {
                  bool isChecked = false;
                  switch (type) {
                    case OfficeCenterFilterAttributeType.applicationList:
                      final exsit = item.applicationList
                          ?.firstWhereOrNull((a) => a == e.value);
                      isChecked = (exsit != null);
                      break;
                    case OfficeCenterFilterAttributeType.processList:
                      final exsit = item.processList
                          ?.firstWhereOrNull((a) => a == e.value);
                      isChecked = (exsit != null);
                      break;
                    case OfficeCenterFilterAttributeType.activityNameList:
                      final exsit = item.activityNameList
                          ?.firstWhereOrNull((a) => a == e.value);
                      isChecked = (exsit != null);
                      break;
                    case OfficeCenterFilterAttributeType.creatorUnitList:
                      final exsit = item.creatorUnitList
                          ?.firstWhereOrNull((a) => a == e.value);
                      isChecked = (exsit != null);
                      break;
                    case OfficeCenterFilterAttributeType.startTimeMonthList:
                      final exsit = item.startTimeMonthList
                          ?.firstWhereOrNull((a) => a == e.value);
                      isChecked = (exsit != null);
                      break;
                    case OfficeCenterFilterAttributeType.completedTimeMonthList:
                      final exsit = item.completedTimeMonthList
                          ?.firstWhereOrNull((a) => a == e.value);
                      isChecked = (exsit != null);
                      break;
                    case OfficeCenterFilterAttributeType.completedList:
                      final exsit = item.completedList
                          ?.firstWhereOrNull((a) => a == e.value);
                      isChecked = (exsit != null);
                      break;
                  }
                  String valueName = e.name ?? '';
                  if (valueName == 'completed') {
                    valueName =
                        'process_office_center_search_filter_completed'.tr;
                  } else if (valueName == 'not completed') {
                    valueName =
                        'process_office_center_search_filter_notcompleted'.tr;
                  }
                  return ActionChip(
                    label: Text(valueName),
                    labelStyle: TextStyle(
                        color: isChecked ? Colors.white : AppColor.primaryText),
                    backgroundColor: isChecked
                        ? Theme.of(context).colorScheme.primary
                        : Colors.white,
                    onPressed: () => controller.clickItem(type, e),
                  );
                }).toList(),
              ))
        ],
      );
    });
  }

  Widget bottomBar(BuildContext context) {
    return SizedBox(
        height: 48,
        child: Row(children: [
          const SizedBox(width: 12),
          Expanded(
              flex: 1,
              child: O2ElevatedButton(
                  () => controller.clickCancel(), Text('reset'.tr, style: Theme.of(context).textTheme.titleSmall),
                  isPrimaryBackgroundColor: false, radius: 8)),
          const SizedBox(width: 10),
          Expanded(
              flex: 1,
              child: O2ElevatedButton(
                  () => controller.clickPositive(), Text('save'.tr), radius: 8)),
          const SizedBox(width: 12),
        ]));
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<OfficeCenterFilterAttributeController>(
      builder: (_) {
        return Scaffold(
          appBar: AppBar(title: Text("process_office_center_search_filter".tr)),
          body: SafeArea(
            child: Column(
              children: [
                Expanded(flex: 1, child: Obx(() {
                  final count = controller.state.applicationList.length + 
                  controller.state.processList.length +
                  controller.state.activityNameList.length +
                  controller.state.creatorUnitList.length +
                  controller.state.startTimeMonthList.length +
                  controller.state.completedTimeMonthList.length +
                  controller.state.completedList.length;
                  if (count > 0) {
                    return _buildView(context);
                  }
                  return O2UI.noResultView(context);
                },)),
                bottomBar(context),
                const SizedBox(height: 8)
              ],
            ),
          ),
        );
      },
    );
  }
}
